﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManageReportVer1.Models;

namespace ManageReportVer1.Controllers
{
     [Authorize(Roles = "Admin")]
    public class DepartmentController : Controller
    {
        //
        // GET: /Department/
        //Add Department
        public ActionResult AddDepartment()
        {
            DepartmentModel department = new DepartmentModel();
            return View(department);
        }
        [HttpPost]
        public ActionResult AddDepartment(DepartmentModel department)
        {
            DBDataContext context = new DBDataContext();
            if (ModelState.IsValid)
            {
                Department departments = new Department();
                departments.DepartmentName = department.DepartmentName;
                departments.DepartmentCode = department.DepartmentCode;

                context.Departments.InsertOnSubmit(departments);
                context.SubmitChanges();
                return RedirectToAction("ViewDepartment","Department");
            }
            return View(department);
        }

        //View Department
        public ActionResult ViewDepartment()
        {
            DBDataContext context = new DBDataContext();
            var department = from s in context.Departments orderby s.DepartmentId ascending select s;
            return View(department.ToList());
        }

        //Update Department
        [HttpGet]
        public ActionResult UpdateDepartment(string id)
        {
            int departmentId = Convert.ToInt32(id);
            DBDataContext context = new DBDataContext();
            Department department = context.Departments.Single(n => n.DepartmentId == departmentId);
            return View(department);
        }

        [HttpPost]
        public ActionResult UpdateDepartment(Department department)
        {
            DBDataContext context = new DBDataContext();
            if (ModelState.IsValid)
            {
                Department departmentUpdate = context.Departments.Single(s => s.DepartmentId == department.DepartmentId);
                departmentUpdate.DepartmentName = department.DepartmentName;
                departmentUpdate.DepartmentCode = department.DepartmentCode;

                context.SubmitChanges();
                return RedirectToAction("ViewDepartment","Department");
            }
            return View(department);
        }

        //Delete Department
        public ActionResult DeleteDepartment(string id)
        {
            int departmentId = Convert.ToInt32(id);
            DBDataContext context = new DBDataContext();
            Department department = context.Departments.Single(n => n.DepartmentId == departmentId);
            context.Departments.DeleteOnSubmit(department);
            context.SubmitChanges();
            return RedirectToAction("ViewDepartment", "Department");
        }

    }
}
